﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.IO;
using System.Xml;

namespace Gerador
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                EnvironmentConfigurationSection environmentConfiguration = EnvironmentConfigurationSection.GetSection();

                foreach (FileConfigurationElement file in environmentConfiguration.Files)
                {
                    Console.WriteLine(string.Format("Lendo Arquivo: {0}", Path.GetFullPath(file.Name)));

                    if (File.Exists(Path.GetFullPath(file.Name)))
                    {
                        XmlDocument doc = new XmlDocument();
                        Dictionary<string, XmlDocument> dic = new Dictionary<string, XmlDocument>();
                        doc.Load(Path.GetFullPath(file.Name));

                        foreach (XPathsElement xpath in environmentConfiguration.Xpaths)
                        {
                            Console.WriteLine(string.Format("Lendo XPath: {0}", xpath.Value));

                            XmlNodeList nodes = doc.SelectNodes(xpath.Value);

                            if (nodes.Count > 0)
                            {
                                foreach (EnvironmentElement environment in xpath.Environments)
                                {
                                    if (dic.ContainsKey(environment.Name))
                                        doc = dic[environment.Name];

                                    foreach (XmlNode node in doc.SelectNodes(xpath.Value))
                                    {
                                        if (xpath.IsEndpoint)
                                        {
                                            UriBuilder uri = new UriBuilder(node.Value);
                                            uri.Host = environment.Value;

                                            if (uri.Port == 80 && uri.Scheme == "http")
                                                uri.Port = -1;

                                            node.Value = uri.ToString();
                                        }
                                        else
                                        {
                                            node.InnerText = environment.Value;
                                        }
                                    }

                                    if (!dic.ContainsKey(environment.Name))
                                        dic.Add(environment.Name, (XmlDocument)doc.Clone());
                                    else
                                        dic[environment.Name] = doc;
                                }
                            }
                        }

                        foreach (var item in dic)
                        {
                            try
                            {
                                Console.WriteLine(string.Format("Salvando Arquivo: {0}", Path.Combine(Path.GetFullPath(file.Destination), Path.GetFileName(file.Name)) + "." + item.Key));

                                item.Value.Save(Path.Combine(Path.GetFullPath(file.Destination), Path.GetFileName(file.Name)) + "." + item.Key);
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine(ex.ToString());
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine(string.Format("Arquivo não existe: {0}", Path.GetFullPath(file.Name)));
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                Console.ReadKey();
            }
        }
    }
}
